package com.usian.controller;

import com.usian.feign.TbItemFeign;
import com.usian.pojo.TbItemParam;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chen
 * @date 2021/1/9 - 19:47 - 星期六
 * @project usian_parent
 * @package com.usian.controller
 * @by GGbomd猪猪侠
 */
@RestController
@RequestMapping("/backend/itemParam")
public class TbItemParamControllerWeb {

    @Autowired
    private TbItemFeign tbItemFeign;

    @RequestMapping("/selectItemParamByItemCatId/{itemCatId}")
    public Result selectItemParamByItemCatId(@PathVariable("itemCatId") Long itemCatId){
        TbItemParam tbItemParam = tbItemFeign.selectItemParamByItemCatId(itemCatId);
        if (tbItemParam!=null){
            return Result.ok(tbItemParam);
        }
        return Result.error("查无结果");
    }

    /**
     * 查询规格模板参数
     * @param page 默认当前页1
     * @param rows 默认显示3条
     * @return
     */
    @RequestMapping("/selectItemParamAll")
    public Result selectItemParamAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "3") Integer rows){
        PageResult pageResult = tbItemFeign.selectItemParamAll(page, rows);
        if (pageResult.getResult().size() > 0){
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    /**
     * 添加商品规格模板 同时操作tb_item_cat表和tb_item_param
     * @param itemCatId
     * @param paramData
     * @return
     */
    @RequestMapping("/insertItemParam")
    public Result insertItemParam(Long itemCatId, String paramData){
        Integer integer = tbItemFeign.insertItemParam(itemCatId, paramData);
        if (integer == 1){
            return Result.ok();
        }
        return Result.error("添加失败：该类目已有规格模板");
    }

    /**
     * 根据id删除商品规格模板
     * @param id
     * @return
     */
    @RequestMapping("/deleteItemParamById")
    public Result deleteItemParamById(@RequestParam Long id){
        Integer integer = tbItemFeign.deleteItemParamById(id);
        if (integer == 1){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
